package cn.kkdlk.dxf;


import cn.kkdlk.enums.GeoTypeEnum;
import cn.kkdlk.enums.StyleClassifyEnum;
import cn.kkdlk.generate.dxf.util.StringUtil;
import cn.kkdlk.model.*;
import cn.kkdlk.parse.DraftDocument;
import cn.kkdlk.parse.common.Color;
import cn.kkdlk.parse.common.Layer;
import cn.kkdlk.parse.common.LineType;
import cn.kkdlk.parse.common.Type;
import cn.kkdlk.parse.dxf.parser.DXFParserBuilder;
import cn.kkdlk.parse.dxf.transformation.DxfLineTransformation;
import cn.kkdlk.parse.dxf.transformation.impl.DxfLineTransformationImpl;
import cn.kkdlk.parse.entities.*;
import cn.kkdlk.parse.math.Point3D;
import cn.kkdlk.parse.parser.ParseException;
import cn.kkdlk.parse.parser.Parser;
import cn.kkdlk.parse.util.StyleFormat;
import cn.kkdlk.transform.CoordinateTransformUtils;
import org.locationtech.proj4j.ProjCoordinate;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Author ytzjj
 * @DateTime 2024/10/12 15:40
 * @Description 解析DXF
 */
public class ParseDXF {


    public static void main(String[] args) throws ParseException {
        ParseDXF parseDXF = new ParseDXF();
        ProjectionDto exploreCadLayer = new ProjectionDto();
        exploreCadLayer.setDegree(3);
        exploreCadLayer.setMeridian(108);

        List<DXFFeatureCollection> dxfLayerList = parseDXF.dxfParse2Geometry("E:\\test\\dxf\\淮南高新区B9地块考古勘探总平面图.dxf", exploreCadLayer);
        System.out.println(dxfLayerList);
    }

    /**
     * 解析CAD文件返回GeoJSON
     *
     * @param filePath
     * @param exploreCadLayer
     * @return
     * @throws ParseException
     */
    public List<DXFFeatureCollection> dxfParse2Geometry(String filePath, ProjectionDto exploreCadLayer) throws ParseException {
        try (FileInputStream stream = new FileInputStream(filePath)) {
            return dxfParse2Geometry(stream, exploreCadLayer);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 解析cad文件，返回geojson
     *
     * @param exploreCadLayer
     * @return
     */
    public List<DXFFeatureCollection> dxfParse2Geometry(InputStream stream, ProjectionDto exploreCadLayer) throws ParseException {
        Parser parser = DXFParserBuilder.createDefaultParser();
        Map<String, Object> properties = new HashMap<>();
        properties.put(DraftDocument.PROPERTY_ENCODING, "UTF-8");
        DraftDocument draftDocument = parser.parse(stream, properties);
        // 获取DXF的所有图层
        Collection<Layer> layers = draftDocument.getLayers();
        List<DXFFeatureCollection> dxfFeatureCollections = new ArrayList<>();
        for (Layer layer : layers) {
            String layerName = layer.getName();
            // DXF图层的features
            List<DXFFeatureDto> features = new ArrayList<>();
            // DXF按图层分类的Features
            DXFFeatureCollection dxfFeatureCollection = new DXFFeatureCollection(layerName, features);

            parse2Text(layer, exploreCadLayer, features);

            parse2MText(layer, exploreCadLayer, features);
            // 点图元
            parse2Point(layer, exploreCadLayer, features);
            // 直线图元
            parse2Line(layer, exploreCadLayer, features);
            // 多段线图元
            parse2LWPolyline(layer, exploreCadLayer, features);
            // 多段线图元
            parse2Polyline(layer, exploreCadLayer, features);
            //曲线图元
            parse2Spline(layer, exploreCadLayer, features);
            // 圆弧
            parse2TArc(layer, exploreCadLayer, features);
            // 圆
            parse2TCircle(layer, exploreCadLayer, features);
            // 椭圆
            parse2TELLIPSE(layer, exploreCadLayer, features);

            dxfFeatureCollections.add(dxfFeatureCollection);
        }
        return dxfFeatureCollections;
    }


    private List<Text> parse2Text(Layer layer, ProjectionDto exploreCadLayer, List<DXFFeatureDto> features) {
        List<Text> texts = layer.getEntitiesByType(Type.TYPE_TEXT);
        for (Text text : texts) {
            Point3D point = text.getInsertPoint();
            double x = point.getX();
            double y = point.getY();
            double z = point.getZ();
            // 数据类型
            String typeName = text.getType().getName();
            String layerName = text.getLayer().getName();
            // 坐标转换
            ProjCoordinate src = new ProjCoordinate(x, y);
            ProjCoordinate srcResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(src, exploreCadLayer.getMeridian(), exploreCadLayer.getDegree());

            // 构建Geometry
            FeatureGeometry featureGeometry = new FeatureGeometry(GeoTypeEnum.POINT.getCode(), Arrays.asList(srcResult.x, srcResult.y, z));
            // 属性
            DXFFeatureProperties properties = new DXFFeatureProperties();
            // 样式
            String textStr = text.getText(); // 文本内容
            // 水平对齐方式  0 左对齐 1居中对齐 2右对齐 3默认对齐 4垂直对齐 5自适应对齐
            int align = text.getAlign(); // 文本对齐方式

            // 垂直对齐方式  0基线对齐 1底部对齐 2 垂直居中对齐 3 顶部对齐
            int valign = text.getValign();
            double rotation = text.getRotation(); // 文本旋转角度
            double height = text.getHeight();
            // 样式
            byte[] colorRGB = text.getColorRGB();// 文本颜色
            String color = Color.getRGBString(text.getColor());
            if (colorRGB != null && colorRGB.length >= 3) {
                color = Color.getRGBString(colorRGB);
            }
            VectorTextBaseStyle vectorTextBaseStyle = new VectorTextBaseStyle();
            vectorTextBaseStyle.setVisibleLabel(true);
            vectorTextBaseStyle.setVisiblePoint(Boolean.FALSE);
            vectorTextBaseStyle.setFontFamily("微软雅黑");
            vectorTextBaseStyle.setFontColor(color);
            vectorTextBaseStyle.setFontSize(height * 2.5);
            vectorTextBaseStyle.setFontStrokeColor("#000000");
            vectorTextBaseStyle.setFontStrokeSize(1D);
            vectorTextBaseStyle.setFontRotation(rotation);
            vectorTextBaseStyle.setTextAlign(StyleFormat.formatAlign(align));
            vectorTextBaseStyle.setClassify(StyleClassifyEnum.TEXT.getClassify());
            vectorTextBaseStyle.setText(textStr);

            properties.setStyle(vectorTextBaseStyle);
            properties.setCadType(typeName);
            properties.setLayerName(layerName);
            properties.setId(String.valueOf(text.getID()));
            properties.setGeometryType(featureGeometry.getType());

            DXFFeatureDto feature = new DXFFeatureDto();
            feature.setGeometry(featureGeometry);
            feature.setProperties(properties);
            features.add(feature);
        }
        return texts;
    }

    private List<MText> parse2MText(Layer layer, ProjectionDto exploreCadLayer, List<DXFFeatureDto> features) {
        List<MText> mTexts = layer.getEntitiesByType(Type.TYPE_MTEXT);
        for (MText mText : mTexts) {
            Point3D point = mText.getInsertPoint();
            double x = point.getX();
            double y = point.getY();
            double z = point.getZ();
            // 数据类型
            String typeName = mText.getType().getName();
            String layerName = mText.getLayer().getName();
            // 坐标转换
            ProjCoordinate src = new ProjCoordinate(x, y);
            ProjCoordinate srcResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(src, exploreCadLayer.getMeridian(), exploreCadLayer.getDegree());

            // 构建Geometry
            FeatureGeometry featureGeometry = new FeatureGeometry(GeoTypeEnum.POINT.getCode(), Arrays.asList(srcResult.x, srcResult.y, z));
            // 属性
            DXFFeatureProperties properties = new DXFFeatureProperties();

            // 样式
            String textStr = mText.getText(); // 文本内容
            //处理文字  {\fFZCuHeiSongS-B-GB|b0|i0|c134|p2;原 王圩村}
            if (StringUtil.isNotBlank(textStr)) {
                textStr = extractPlainText(textStr);
            }

            // 水平对齐方式  0 左对齐 1居中对齐 2右对齐 3默认对齐 4垂直对齐 5自适应对齐
            int align = mText.getAlign(); // 文本对齐方式
            // 垂直对齐方式  0基线对齐 1底部对齐 2 垂直居中对齐 3 顶部对齐
            int valign = mText.getValign();
            double rotation = mText.getRotation(); // 文本旋转角度
            double height = mText.getHeight();
            // 样式
            byte[] colorRGB = mText.getColorRGB();// 文本颜色
            String color = Color.getRGBString(mText.getColor());
            if (colorRGB != null && colorRGB.length >= 3) {
                color = Color.getRGBString(colorRGB);
            }

            VectorTextBaseStyle vectorTextBaseStyle = new VectorTextBaseStyle();
            vectorTextBaseStyle.setVisibleLabel(true);
            vectorTextBaseStyle.setVisiblePoint(Boolean.FALSE);
            vectorTextBaseStyle.setFontFamily("微软雅黑");
            vectorTextBaseStyle.setFontColor(color);
            vectorTextBaseStyle.setFontSize(height * 2.5);
            ;
            vectorTextBaseStyle.setFontStrokeColor("#000000");
            vectorTextBaseStyle.setFontStrokeSize(1D);
            vectorTextBaseStyle.setFontRotation(rotation);
            vectorTextBaseStyle.setTextAlign(StyleFormat.formatAlign(align));
            vectorTextBaseStyle.setClassify(StyleClassifyEnum.TEXT.getClassify());
            vectorTextBaseStyle.setText(textStr);

            properties.setStyle(vectorTextBaseStyle);
            properties.setCadType(typeName);
            properties.setLayerName(layerName);
            properties.setId(String.valueOf(mText.getID()));
            properties.setGeometryType(featureGeometry.getType());


            DXFFeatureDto feature = new DXFFeatureDto();
            feature.setGeometry(featureGeometry);
            feature.setProperties(properties);
            features.add(feature);
        }
        return mTexts;
    }

    private List<Point> parse2Point(Layer layer, ProjectionDto exploreCadLayer, List<DXFFeatureDto> features) {
        List<Point> points = layer.getEntitiesByType(Type.TYPE_POINT);
        for (Point point : points) {
            double x = point.getX();
            double y = point.getY();
            double z = point.getZ();
            // 数据类型
            String typeName = point.getType().getName();
            String layerName = point.getLayer().getName();
            // 坐标转换
            ProjCoordinate src = new ProjCoordinate(x, y);
            ProjCoordinate srcResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(src, exploreCadLayer.getMeridian(), exploreCadLayer.getDegree());

            // 构建Geometry
            FeatureGeometry featureGeometry = new FeatureGeometry(GeoTypeEnum.POINT.getCode(), Arrays.asList(srcResult.x, srcResult.y, z));
            // 属性
            DXFFeatureProperties properties = new DXFFeatureProperties();

            // 样式
            byte[] colorRGB = point.getColorRGB();// 文本颜色
            String color = Color.getRGBString(point.getColor());
            if (colorRGB != null && colorRGB.length >= 3) {
                color = Color.getRGBString(colorRGB);
            }

            VectorPointBaseStyle vectorPointBaseStyle = new VectorPointBaseStyle();
            vectorPointBaseStyle.setVisiblePoint(true);
            vectorPointBaseStyle.setIconName("\\ue6c3");
            vectorPointBaseStyle.setIconType("VectorIcon");
            vectorPointBaseStyle.setIconSize(Text.DEFAULT_FONT_SIZE * 2.5);
            vectorPointBaseStyle.setIconColor(color);
            vectorPointBaseStyle.setIconClass("iconplot");
            vectorPointBaseStyle.setIconStrokeColor("#FFFFFF");
            vectorPointBaseStyle.setIconStrokeSize(1D);
            vectorPointBaseStyle.setClassify(StyleClassifyEnum.POINT.getClassify());
            vectorPointBaseStyle.setText("");
            vectorPointBaseStyle.setVisibleLabel(false);

            properties.setStyle(vectorPointBaseStyle);
            properties.setCadType(typeName);
            properties.setLayerName(layerName);
            properties.setId(String.valueOf(point.getID()));
            properties.setGeometryType(featureGeometry.getType());


            DXFFeatureDto feature = new DXFFeatureDto();
            feature.setGeometry(featureGeometry);
            feature.setProperties(properties);
            features.add(feature);
        }
        return points;
    }


    private List<Line> parse2Line(Layer layer, ProjectionDto exploreCadLayer, List<DXFFeatureDto> features) {
        List<Line> lines = layer.getEntitiesByType(Type.TYPE_LINE);
        for (Line line : lines) {
            double startX = line.getStartPoint().getX();
            double startY = line.getStartPoint().getY();
            double startZ = line.getStartPoint().getZ();
            double endX = line.getEndPoint().getX();
            double endY = line.getEndPoint().getY();
            double endZ = line.getEndPoint().getZ();

            // 数据类型
            String typeName = line.getType().getName();
            String layerName = line.getLayer().getName();
            // 坐标转换
            ProjCoordinate start = new ProjCoordinate(startX, startY);
            ProjCoordinate end = new ProjCoordinate(endX, endY);

            ProjCoordinate startResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(start, exploreCadLayer.getMeridian(), exploreCadLayer.getDegree());
            ProjCoordinate endResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(end, exploreCadLayer.getMeridian(), exploreCadLayer.getDegree());

            // 构建Geometry
            List<Double> startPoint = Arrays.asList(startResult.x, startResult.y, startZ);
            List<Double> endPoint = Arrays.asList(endResult.x, endResult.y, endZ);

            FeatureGeometry featureGeometry = new FeatureGeometry(GeoTypeEnum.LINE_STRING.getCode(), Arrays.asList(startPoint, endPoint));
            // 属性
            DXFFeatureProperties properties = new DXFFeatureProperties();

            // 样式
            int lineWeight = line.getLineWeight();// 线条粗细
            double length = line.getLength(); // 线段长度
            LineType lineType = line.getLineType(); // 线条类型 "CONTINUOUS"（实线），"DASHED"（虚线），"DOT"（点线
            byte[] colorRGB = line.getColorRGB();// 文本颜色
            String color = Color.getRGBString(line.getColor());
            if (colorRGB != null && colorRGB.length >= 3) {
                color = Color.getRGBString(colorRGB);
            }
            VectorLineStringBaseStyle vectorLineStringBaseStyle = new VectorLineStringBaseStyle();
            vectorLineStringBaseStyle.setVisibleLineString(true);
            vectorLineStringBaseStyle.setVisibleLabel(false);
            vectorLineStringBaseStyle.setLineStringColor(color);
            vectorLineStringBaseStyle.setLineStringWidth(lineWeight <= 0 ? 1D : lineWeight);
            vectorLineStringBaseStyle.setLineStringLineDash(lineType.getPattern());
            vectorLineStringBaseStyle.setClassify(StyleClassifyEnum.LINESTRING.getClassify());
            vectorLineStringBaseStyle.setText("");

            properties.setLength(length);
            properties.setStyle(vectorLineStringBaseStyle);
            properties.setCadType(typeName);
            properties.setLayerName(layerName);
            properties.setId(String.valueOf(line.getID()));
            properties.setGeometryType(featureGeometry.getType());

            DXFFeatureDto feature = new DXFFeatureDto();
            feature.setGeometry(featureGeometry);
            feature.setProperties(properties);
            features.add(feature);

        }
        return lines;
    }

    private List<LWPolyline> parse2LWPolyline(Layer layer, ProjectionDto exploreCadLayer, List<DXFFeatureDto> features) {
        List<LWPolyline> lwPolylines = layer.getEntitiesByType(Type.TYPE_LWPOLYLINE);
        this.parse2LWPolylineHandler(lwPolylines, exploreCadLayer, features);
        return lwPolylines;
    }


    private List<Polyline> parse2Polyline(Layer layer, ProjectionDto exploreCadLayer, List<DXFFeatureDto> features) {
        List<Polyline> polylines = layer.getEntitiesByType(Type.TYPE_POLYLINE);
        for (Polyline polyline : polylines) {

            // 数据类型
            String typeName = polyline.getType().getName();
            String layerName = polyline.getLayer().getName();


            List<List<Double>> lines = new ArrayList<>();
            polyline.getVertices().forEach(vertex -> {
                Point3D point = vertex.getPoint();
                double x = point.getX();
                double y = point.getY();
                double z = point.getZ();
                // 坐标转换
                ProjCoordinate src = new ProjCoordinate(x, y);
                ProjCoordinate srcResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(src, exploreCadLayer.getMeridian(), exploreCadLayer.getDegree());
                lines.add(Arrays.asList(srcResult.x, srcResult.y, z));
            });

            // 正常是线
            FeatureGeometry featureGeometry = new FeatureGeometry(GeoTypeEnum.LINE_STRING.getCode(), lines);

            // 样式
            byte[] colorRGB = polyline.getColorRGB();// 线条颜色
            String color = Color.getRGBString(polyline.getColor());
            if (colorRGB != null && colorRGB.length >= 3) {
                color = Color.getRGBString(colorRGB);
            }
            int lineWeight = polyline.getLineWeight();// 线条粗细
            double length = polyline.getLength(); // 线段长度
            LineType lineType = polyline.getLineType(); // 线条类型 "CONTINUOUS"（实线），"DASHED"（虚线），"DOT"（点线
            Double[] pattern = lineType.getPattern();
            // 属性
            DXFFeatureProperties properties = new DXFFeatureProperties();

            // 判断是否闭合
            if (polyline.isClosed()) {
                // 闭合了就是面,闭合数据
                lines.add(new ArrayList<>(lines.get(0)));
                featureGeometry.setType(GeoTypeEnum.POLYGON.getCode());
                featureGeometry.setCoordinates(Collections.singletonList(lines));
                VectorPolygonBaseStyle style = new VectorPolygonBaseStyle();
                style.setVisiblePolygon(true);
                style.setPolygonFillColor("#FFFFFF42");
                style.setPolygonBorderWidth(lineWeight <= 0 ? 1D : lineWeight);
                style.setPolygonBorderColor(color);
                style.setPolygonBorderLineDash(pattern);
                style.setClassify(StyleClassifyEnum.POLYGON.getClassify());
                style.setText("");
                style.setVisibleLabel(false);
                properties.setStyle(style);
            } else {
                VectorLineStringBaseStyle style = new VectorLineStringBaseStyle();
                style.setVisibleLineString(true);
                style.setLineStringColor(color);
                style.setLineStringWidth(lineWeight <= 0 ? 1D : lineWeight);
                style.setLineStringLineDash(pattern);
                style.setClassify(StyleClassifyEnum.LINESTRING.getClassify());
                style.setText("");
                style.setVisibleLabel(false);
                properties.setStyle(style);
            }
            DXFFeatureDto feature = new DXFFeatureDto();
            feature.setGeometry(featureGeometry);

            properties.setCadType(typeName);
            properties.setLayerName(layerName);
            properties.setId(String.valueOf(polyline.getID()));
            properties.setGeometryType(featureGeometry.getType());
            properties.setLength(length);


            feature.setProperties(properties);
            features.add(feature);
        }
        return polylines;
    }

    private List<Spline> parse2Spline(Layer layer, ProjectionDto exploreCadLayer, List<DXFFeatureDto> features) {
        List<Spline> splines = layer.getEntitiesByType(Type.TYPE_SPLINE);
        for (Spline spline : splines) {

            // 数据类型
            String typeName = spline.getType().getName();
            String layerName = spline.getLayer().getName();

            List<List<Double>> lines = new ArrayList<>();
            spline.getSplinePoints().forEach(point -> {
                double x = point.getX();
                double y = point.getY();
                double z = point.getZ();
                // 坐标转换
                ProjCoordinate src = new ProjCoordinate(x, y);
                ProjCoordinate srcResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(src, exploreCadLayer.getMeridian(), exploreCadLayer.getDegree());
                lines.add(Arrays.asList(srcResult.x, srcResult.y, z));
            });

            // 正常是线
            FeatureGeometry featureGeometry = new FeatureGeometry(GeoTypeEnum.LINE_STRING.getCode(), lines);

            // 样式
            byte[] colorRGB = spline.getColorRGB();// 线条颜色
            String color = Color.getRGBString(spline.getColor());
            if (colorRGB != null && colorRGB.length >= 3) {
                color = Color.getRGBString(colorRGB);
            }
            int lineWeight = spline.getLineWeight();// 线条粗细
            double length = spline.getLength(); // 线段长度
            LineType lineType = spline.getLineType(); // 线条类型 "CONTINUOUS"（实线），"DASHED"（虚线），"DOT"（点线
            Double[] pattern = lineType.getPattern();
            // 属性
            DXFFeatureProperties properties = new DXFFeatureProperties();
            // 判断是否闭合
            if (spline.isClosed()) {
                // 闭合了就是面,闭合数据
                lines.add(new ArrayList<>(lines.get(0)));
                featureGeometry.setType(GeoTypeEnum.POLYGON.getCode());
                featureGeometry.setCoordinates(Collections.singletonList(lines));
                VectorPolygonBaseStyle style = new VectorPolygonBaseStyle();
                style.setVisiblePolygon(true);
                style.setPolygonFillColor("#FFFFFF42");
                style.setPolygonBorderWidth(lineWeight <= 0 ? 1D : lineWeight);
                style.setPolygonBorderColor(color);
                style.setPolygonBorderLineDash(pattern);
                style.setClassify(StyleClassifyEnum.POLYGON.getClassify());
                style.setText("");
                style.setVisibleLabel(false);
                properties.setStyle(style);
            } else {
                VectorLineStringBaseStyle style = new VectorLineStringBaseStyle();
                style.setVisibleLineString(true);
                style.setLineStringColor(color);
                style.setLineStringWidth(lineWeight <= 0 ? 1D : lineWeight);
                style.setLineStringLineDash(pattern);
                style.setClassify(StyleClassifyEnum.LINESTRING.getClassify());
                style.setText("");
                style.setVisibleLabel(false);
                properties.setStyle(style);
            }
            properties.setCadType(typeName);
            properties.setLayerName(layerName);
            properties.setId(String.valueOf(spline.getID()));
            properties.setGeometryType(featureGeometry.getType());
            properties.setLength(length);


            DXFFeatureDto feature = new DXFFeatureDto();
            feature.setGeometry(featureGeometry);
            feature.setProperties(properties);
            features.add(feature);
        }
        return splines;
    }

    /**
     * 椭圆
     *
     * @param layer
     * @param projectionDto
     * @param features
     */
    private List<Ellipse> parse2TELLIPSE(Layer layer, ProjectionDto projectionDto, List<DXFFeatureDto> features) {
        List<Ellipse> ellipses = layer.getEntitiesByType(Type.TYPE_ELLIPSE);
        return ellipses;
    }

    /**
     * 圆
     *
     * @param layer
     * @param projectionDto
     * @param features
     */
    private List<Circle> parse2TCircle(Layer layer, ProjectionDto projectionDto, List<DXFFeatureDto> features) {
        List<Circle> circles = layer.getEntitiesByType(Type.TYPE_CIRCLE);
        // 将几何圆、转换为点圆线数据
        DxfLineTransformation dxfLineTransformation = DxfLineTransformationImpl.getSingleInstance();
        List<LWPolyline> lwPolylines = dxfLineTransformation.circleTransform(circles);
        this.parse2LWPolylineHandler(lwPolylines, projectionDto, features);
        return circles;
    }

    /**
     * 圆弧
     *
     * @param layer
     * @param projectionDto
     * @param features
     */
    private List<Arc> parse2TArc(Layer layer, ProjectionDto projectionDto, List<DXFFeatureDto> features) {
        List<Arc> arcs = layer.getEntitiesByType(Type.TYPE_ARC);
        // 将几何弧线、转换为点弧线数据
        DxfLineTransformation dxfLineTransformation = DxfLineTransformationImpl.getSingleInstance();
        List<LWPolyline> lwPolylines = dxfLineTransformation.arcTransform(arcs);
        this.parse2LWPolylineHandler(lwPolylines, projectionDto, features);
        return arcs;
    }


    /**
     * 解析多段线
     *
     * @param lwPolylines
     * @param projectionDto
     * @param features
     */
    private void parse2LWPolylineHandler(List<LWPolyline> lwPolylines, ProjectionDto projectionDto, List<DXFFeatureDto> features) {
        for (LWPolyline lwPolyline : lwPolylines) {
            // 数据类型
            String typeName = lwPolyline.getType().getName();
            String layerName = lwPolyline.getLayer().getName();
            List<List<Double>> lines = new ArrayList<>();
            lwPolyline.getVertices().forEach(vertice -> {
                double x = vertice.getX();
                double y = vertice.getY();
                // 坐标转换
                ProjCoordinate src = new ProjCoordinate(x, y);
                ProjCoordinate srcResult = CoordinateTransformUtils.cgcs2000ToWGS84ByL0BandWidth(src, projectionDto.getMeridian(), projectionDto.getDegree());
                lines.add(Arrays.asList(srcResult.x, srcResult.y));
            });

            // 正常是线
            FeatureGeometry featureGeometry = new FeatureGeometry(GeoTypeEnum.LINE_STRING.getCode(), lines);

            // 样式
            byte[] colorRGB = lwPolyline.getColorRGB();// 线条颜色
            String color = Color.getRGBString(lwPolyline.getColor());
            if (colorRGB != null && colorRGB.length >= 3) {
                color = Color.getRGBString(colorRGB);
            }
            int lineWeight = lwPolyline.getLineWeight();// 线条粗细
            double length = lwPolyline.getLength(); // 线段长度
            LineType lineType = lwPolyline.getLineType(); // 线条类型 "CONTINUOUS"（实线），"DASHED"（虚线），"DOT"（点线）
            Double[] pattern = lineType.getPattern();


            // 属性
            DXFFeatureProperties properties = new DXFFeatureProperties();
            // 判断是否闭合
            if (lwPolyline.isClosed()) {
                // 闭合了就是面,闭合数据
                lines.add(new ArrayList<>(lines.get(0)));
                featureGeometry.setType(GeoTypeEnum.POLYGON.getCode());
                featureGeometry.setCoordinates(Collections.singletonList(lines));
                VectorPolygonBaseStyle style = new VectorPolygonBaseStyle();
                style.setVisiblePolygon(true);
                style.setPolygonFillColor("#FFFFFF42");
                style.setPolygonBorderWidth(lineWeight <= 0 ? 1D : lineWeight);
                style.setPolygonBorderColor(color);
                style.setPolygonBorderLineDash(pattern);
                style.setClassify(StyleClassifyEnum.POLYGON.getClassify());
                style.setText("");
                style.setVisibleLabel(false);
                properties.setStyle(style);
            } else {
                VectorLineStringBaseStyle style = new VectorLineStringBaseStyle();
                style.setVisibleLineString(true);
                style.setLineStringColor(color);
                style.setLineStringWidth(lineWeight <= 0 ? 1D : lineWeight);
                style.setLineStringLineDash(pattern);
                style.setClassify(StyleClassifyEnum.LINESTRING.getClassify());
                style.setText("");
                style.setVisibleLabel(false);
                properties.setStyle(style);
            }
            properties.setCadType(typeName);
            properties.setLayerName(layerName);
            properties.setId(String.valueOf(lwPolyline.getID()));
            properties.setGeometryType(featureGeometry.getType());
            properties.setLength(length);

            DXFFeatureDto feature = new DXFFeatureDto();
            feature.setGeometry(featureGeometry);
            feature.setProperties(properties);
            features.add(feature);
        }
    }


    //获取MText的实际内容
    private static String extractPlainText(String mtext) {
        // 去掉花括号 {} 包裹的内容以及其中的格式控制字符串
        String plainText = mtext.replaceAll("\\{\\\\[^;]+;|\\}", "");

        // 去掉颜色控制代码（如 \C4;）
        plainText = plainText.replaceAll("\\\\C\\d+;", "");
        plainText = plainText.replaceAll("\\\\P", "");
        plainText = plainText.replaceAll("\\\\pi\\d*\\.?\\d*;", "");
        plainText = plainText.replaceAll("\\\\f[^;]*;", "");

        // 去除多余的空白字符
        plainText = plainText.trim();

        return plainText;
    }


}
